﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EncuentraloSilverlight.ServiceReference2;
using System.Windows.Navigation;
using System.Windows.Browser;
using System.Collections.Generic;

namespace EncuentraloSilverlight
{
	public partial class Seller_View : Page
	{
        Seller seller;
		public Seller_View()
		{
			// Required to initialize variables
			InitializeComponent();
            FillDatagrid();
		}

        private void FillDatagrid()
        {
            Service2Client webservice = new Service2Client();
            webservice.FindSellerFromNameCompleted += (obj, eventarg) =>
            {
                if (eventarg.Result.Count > 0)
                {
                    seller = eventarg.Result[0];
                    Name_Textbox.Text = seller.Name;
                    Address_TextBox.Text = seller.Address.Street + " " + seller.Address.Number + ",\n" + seller.Address.Commune + ", " + seller.Address.City;
                    if (seller.Products != null)
                    {
                        Products_Datagrid.ItemsSource = seller.Products;
                        Products_Datagrid.Columns.CollectionChanged += (objj, eventargg) =>
                        {
                            if (Products_Datagrid.Columns.Count > 7)
                            {
                                Products_Datagrid.MinColumnWidth = 0;
                                Products_Datagrid.Columns[0].Width = new DataGridLength(0);
                                Products_Datagrid.Columns[1].Header = "Precio";
                                Products_Datagrid.Columns[2].Header = "Nombre";
                                Products_Datagrid.Columns[3].Header = "Descripción";
                                Products_Datagrid.Columns[4].Width = new DataGridLength(0);
                                Products_Datagrid.Columns[5].Width = new DataGridLength(0);
                                Products_Datagrid.Columns[6].Width = new DataGridLength(0);
                                Products_Datagrid.Columns[7].Width = new DataGridLength(0);

                            }
                        };
                    }
                }
            };
            webservice.FindSellerFromNameAsync(Cookie.GetCookie("EncuentraloUser"));
        }
        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Cookie.SetCookie("EncuentraloUser", "");
            NavigationService.Navigate(new Uri("/Views/Home.xaml", UriKind.Relative));
        }


        private void KeyUp_TextBox_CommonExpressSearch(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Cookie.SetCookie("EncuentraloSearch", TextBox_CommonExpressSearch.Text);
                NavigationService.Navigate(new Uri("/Views/BusquedaExpress.xaml", UriKind.Relative));
            }
        }

        private void Click_Button_CommonExpressSearch(object sender, RoutedEventArgs e)
        {
            Cookie.SetCookie("EncuentraloSearch", TextBox_CommonExpressSearch.Text);
            NavigationService.Navigate(new Uri("/Views/BusquedaExpress.xaml", UriKind.Relative));
        }

        // agregar producto
        private void Agregar_Producto(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AddProduct.xaml", UriKind.Relative));
        }


        private void Eliminar_Producto(object sender, RoutedEventArgs e)
        {
            Service2Client webservice = new Service2Client();
            int selectedIndex = Products_Datagrid.SelectedIndex;
            if (selectedIndex < 0)
            {
                MessageBox.Show("No hay un producto seleccionado para eliminar");
            }
            else
            {
                webservice.DeleteProductCompleted += (obj1, eventarg1) =>
                {
                    MessageBox.Show("Producto eliminado con exito");
                    UpdateDatagrid(Products_Datagrid);
                };
                Product p1 = Products_Datagrid.SelectedItem as Product;
                webservice.DeleteProductAsync(p1);               
                
            }      
        }

        private void Editar_Producto(object sender, RoutedEventArgs e)
        {
            Product selectedProduct = Products_Datagrid.SelectedItem as Product;
            Cookie.SetCookie("SelectedProduct", selectedProduct.Name + "," + selectedProduct.Price + "," + selectedProduct.Description);
            NavigationService.Navigate(new Uri("/Views/EditProduct.xaml", UriKind.Relative));
        }

        /// <summary>
        /// Update de un datagrid de productos
        /// </summary>
        /// <param name="datagridToUpdate"></param>
        private void UpdateDatagrid(DataGrid datagridToUpdate)
        {
            Service2Client webservice = new Service2Client();
            webservice.FindSellerFromNameCompleted += (obj2, eventarg2) =>
            {
                if (eventarg2.Result.Count > 0)
                {
                    
                    seller = eventarg2.Result[0];
                    if (seller.Products != null)
                    {
                        datagridToUpdate.ItemsSource = seller.Products;
                        datagridToUpdate.Columns.CollectionChanged += (obj3, eventarg3) =>
                        {
                            if (Products_Datagrid.Columns.Count > 7)
                            {
                                datagridToUpdate.MinColumnWidth = 0;
                                datagridToUpdate.Columns[0].Width = new DataGridLength(0);
                                datagridToUpdate.Columns[1].Header = "Precio";
                                datagridToUpdate.Columns[2].Header = "Nombre";
                                datagridToUpdate.Columns[3].Header = "Descripción";
                                datagridToUpdate.Columns[4].Width = new DataGridLength(0);
                                datagridToUpdate.Columns[5].Width = new DataGridLength(0);
                                datagridToUpdate.Columns[6].Width = new DataGridLength(0);
                                Products_Datagrid.Columns[7].Width = new DataGridLength(0);

                            }
                        };
                    }
                }
            };
            webservice.FindSellerFromNameAsync(Cookie.GetCookie("EncuentraloUser"));

        }

        private void Ver_Producto(object sender, RoutedEventArgs e)
        {
            Product selectedProduct = Products_Datagrid.SelectedItem as Product;
            Cookie.SetCookie("SelectedProduct", selectedProduct.Name + "," + selectedProduct.Price + "," + selectedProduct.Description);
            NavigationService.Navigate(new Uri("/Views/ViewProduct.xaml", UriKind.Relative));
        }

        private void Products_Datagrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space && Products_Datagrid.SelectedIndex >= 0)
            {
                Product selectedProduct = Products_Datagrid.SelectedItem as Product;
                Cookie.SetCookie("SelectedProduct", selectedProduct.Name + "," + selectedProduct.Price + "," + selectedProduct.Description);
                NavigationService.Navigate(new Uri("/Views/ViewProduct.xaml", UriKind.Relative));
            }
        }

      
	}
}